package com.avego.game;

import java.util.HashMap;
import java.util.Map;

public class Referee
{
	Map<Player, Integer> mPlayersYellowCard;
	private Board mBoard;

	public Referee()
	{
		mPlayersYellowCard = new HashMap<Player, Integer>();
	}

	
	
	/**
	 * Check if  player can return to game
	 *
	 * @param player the player
	 * @return true, if the player can return to game
	 */
	public boolean canPlayerReturnToGame(Player player)
	{
		Integer yellowCards = mPlayersYellowCard.get(player);

		if (yellowCards == null || yellowCards < 4)
			return true;
		else
		{
				mBoard.removePlayerFromGame(player);
				mBoard.checkForWinner();
				return false;
		}
	}


	/**
	 * Validate a player move.
	 *
	 * @param player the player
	 * @return true, if its a valid  move
	 */
	public boolean validatePlayerMove(Player player)
	{
		boolean hasPlayers = mBoard.hasPlayersOnRadius(player, 2);

		if (hasPlayers)
		{
			Integer yellowCards = mPlayersYellowCard.get(player);

			if (yellowCards == null)
				mPlayersYellowCard.put(player, new Integer(1));
			else
			{
				yellowCards++;
				mPlayersYellowCard.put(player, yellowCards);

				if (yellowCards % 2 == 0)
					suspendPlayer(player);
			}
			
			return false;
		}
		return true;
	}
	
	
	/**
	 * Suspend player.
	 *
	 * @param player the player
	 */
	private void suspendPlayer(Player player)
	{
		player.setPlayerAsSuspended();
		mBoard.removePlayerFromBoard(player);
	}

	/**
	 * Count the numbers of yellows cards of a player.
	 *
	 * @param player the player
	 * @return the int
	 */
	public int countPlayerYellowCard(Player player) {
		return mPlayersYellowCard.get(player);
	}

	public Board getBoard() {
		return mBoard;
	}

	public void setBoard(Board board) {
		this.mBoard = board;
	}
}
